function $ajax(method, url, callback) {
  var xhr = new XMLHttpRequest();
  xhr.open(method, url);
  xhr.addEventListener("readystatechange", function () {
    if (xhr.readyState == 4 && xhr.status == 200) {
      var res = JSON.parse(xhr.response);
      // 得到相应执行结果后执行的内容,将响应数据传递给回调函数
      callback(res);
    }
  });
  xhr.send();
}
var domain = "http://wheel.couchpotato.fun/";
var api = {
  get_prize: "index/wheel/get_prize_list",
  get_record: "index/wheel/get_draw_record_list",
  get_top_record: "index/wheel/get_top_draw_record_list",
  draw: "index/wheel/draw",
};

function get_prize_cb(res) {
  var data = res.data.prize_list;
  console.log(res);

  var itemList = document.querySelectorAll(".yuan-1 div[class*=item]");
  itemList.forEach(function (el, idx) {
    var p = el.querySelector("p"),
      img = el.querySelector("img");
    p.innerHTML = data[idx].name;
    img.src = data[idx].diagram;
  });
}
$ajax("GET", domain + api.get_prize, get_prize_cb);

function get_prize_user(res) {
  var swiperSlide = document.querySelector(".swiper-wrapper");
  console.log(swiperSlide);

  //取10条数据作为轮播数据
  var swiperData = res.data.slice(0, 10);
  console.log(swiperData);
  // var df = document.createDocumentFragment();
  // df.append(swiperData)
  function format(num) {
    return num.slice(0, 3) + "***" + num.slice(9, 11);
  }
  //  for (var i = 0; i < swiperData.length; i++) {
  //   var tep = `<div class="swiper-slide"><i class="iconfont icon-lingdang"></i>恭喜 ${format(
  //     swiperData[i].phone
  //   )} 用户抽中 ${swiperData[i].prize_name}</div>`;
  //   swiperSlide.innerHTML += tep;
  // }

  swiperData.forEach(function (el) {
    var tep = `<div class="swiper-slide"><i class="iconfont icon-lingdang"></i>恭喜 ${format(
          el.phone
    )} 用户抽中 ${el.prize_name}</div>`;
    swiperSlide.innerHTML += tep;
})

  var mySwiper = new Swiper(".swiper-container", {
    direction: "vertical", // 垂直切换选项
    loop: true, // 循环模式选项
    autoplay: true,
    observer: true, // 开启观察模式,当 slide 或 swiper 变化时,重新实例化swiper
    observeSlideChildren:true,
  });

 
}

$ajax("GET", domain + api.get_top_record, get_prize_user);

function get_prize_list(res) {
  var list = res.data.draw_record_list;
  console.log(res);
  var tablelist = document.querySelector("table tbody");
  console.log(tablelist);
  // var dataObj = JSON.stringify(list);
  idx = 0;
  // console.log(dataObj);
  function format(timestamp) {
    var date = new Date(timestamp * 1000); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
    Y = date.getFullYear() + ".";
    M =
      (date.getMonth() + 1 < 10
        ? "0" + (date.getMonth() + 1)
        : date.getMonth() + 1) + ".";
    D = (date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) + " ";
    h = (date.getHours() < 10 ? "0" + date.getHours() : date.getHours()) + ":";
    m = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
    return Y + M + D + h + m;
  }
  list.forEach(function (el, itm) {
    var tmp = `<tr>
    <td>${format(el.draw_time_stamp)}</td>
    <td>${el.prize_name}</td>
  </tr>`;
    tablelist.innerHTML += tmp;
  });
}
$ajax("GET", domain + api.get_record, get_prize_list);

var btn = document.querySelector(".ny-1 .ny-2");
var yuan = document.querySelector(".yuan-1");
var popup = document.querySelector(".popup");
var text = popup.querySelector("p");
var close = popup.querySelector("button");
var minRotate = 1800;
var prize = [-30, -90, -150, -210, -270, -330];
var sumRotate = 0; //用于增量旋转角度
var count = 0; //记录统计次数，用于修正次数
var list = ["苹果手机", "苹果平板", "water", "water", "比特币", "water"];
var bingo_id;
function drawFn() {
  btn.removeEventListener("click", drawFn);
  var phone = "13129336627";
  var xhr = new XMLHttpRequest();
  xhr.open(
    "GET",
    `http://wheel.couchpotato.fun/index/wheel/draw?phone=${phone}`
  );
  xhr.addEventListener("readystatechange", function () {
    if (xhr.readyState == 4 && xhr.status == 200) {
      // 得到相应执行结果后执行的内容,将响应数据传递给回调函数
      console.log(xhr.response);

      bingo_id = JSON.parse(xhr.response).data.bingo_prize_id;
      console.log(bingo_id);
      (bingo_id == 6 || bingo_id == 7 || bingo_id == 8) && (bingo_id = 5);
      console.log(bingo_id);
     count++;
      sumRotate = count * minRotate + prize[bingo_id];
     // sumRotate =minRotate + prize[bingo_id];
      yuan.style.transform = `rotate(${sumRotate}deg)`;
    }
  });
  xhr.send();
}
yuan.addEventListener("transitionend", function (e) {
  btn.addEventListener("click", drawFn);
  // var tem = `恭喜你抽中了${list[bingo_id]}`;
  // text.innerHTML = tem;
  // popup.style.display = "block";
});
close.addEventListener("click", function () {
  popup.style.display = "none";
});
btn.addEventListener("click", drawFn);
